{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#数组合并 与 拆分\n",
    "#合并前提是两个数组的元素个数相同。\n",
    "#合并后原数组不变，需要用新的变量承接合并结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3],\n",
       "       [ 4,  5,  6,  7],\n",
       "       [ 8,  9, 10, 11]])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a=np.arange(12).reshape(3,4)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([9, 8, 3, 8], dtype=int32)"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b=np.random.randint(0,10,size=4)\n",
    "b"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "垂直方向的合并：np.hstack()  np.concatenate((a, b), axis=0)\n",
    "垂直方向合并是指：列数相同的两个数组合并为一个数组，需要维度和列数相同。\n",
    "合并的结果是垂直方向添加行数."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3],\n",
       "       [ 4,  5,  6,  7],\n",
       "       [ 8,  9, 10, 11],\n",
       "       [ 9,  8,  3,  8]])"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b=b.reshape(1,4) # 调整数组形状\n",
    "np.concatenate((a, b), axis=0) # 合并数组，axis=0表示沿着行方向合并\n",
    "np.vstack((a, b)) # 垂直方向合并 =再加一行  等价于concatenate((a, b.reshape(1,4)), axis=0) 但是该方法不需要reshape"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "水平方向合并：np.hstack()  concatenate((a, b.reshape(1,4)), axis=1)\n",
    "合并的前提是两个数组的维度相同，行数相同\n",
    "结果是，在水平方向上添加了新的列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[  0,   1,   2,   3, 666, 666],\n",
       "       [  4,   5,   6,   7, 666, 666],\n",
       "       [  8,   9,  10,  11, 666, 666]])"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "c=np.full((3,2), 666) #创建3行2列的全666数组\n",
    "np.hstack((a, c)) # 水平方向合并\n",
    "# np.split(a, 3, axis=1) # 拆分数组，axis=1表示沿着列方向拆分，3表示分成3列\n",
    "# np.hsplit(a, 2) # 水平方向拆分，2表示分成2列\n",
    "# np.vsplit(a, 2) # 垂直方向拆分，2表示分成2行\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "数组的拆分"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2],\n",
       "       [ 4,  5,  6],\n",
       "       [ 8,  9, 10]])"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1,arr3= np.split(a,[4],axis=0)  #分割数组a，分割点为4，axis=0表示按行分割  当分割点大于行数时，不会报错，会返回原数组\n",
    "arr2,arr4= np.split(a,[3],axis=1)  #分割数组a，分割点为4，axis=1表示按列分割\n",
    "arr2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([[0, 1, 2, 3],\n",
       "        [4, 5, 6, 7]]),\n",
       " array([[ 8,  9, 10, 11]])]"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.vsplit(a,[2]) #垂直分割，分割点为2，[0:2] [2:] 表示分割成两个数组，第一个数组包含前2行(不包含序号为2的行)，第二个数组包含后面的行 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([[0, 1, 2, 3]]),\n",
       " array([[ 4,  5,  6,  7],\n",
       "        [ 8,  9, 10, 11]]),\n",
       " array([[12, 13, 14, 15]])]"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d=np.arange(16).reshape(4,4)\n",
    "np.vsplit(d,[1,3]) # 垂直分割，返回3个数组 #[0:1] [1:3] [3:] 0-1不包含1，1-3包含1不包含3，3-end包含3"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "machineLearn",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
